package cn.lysy.xiaoruan.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.lysy.xiaoruan.MainActivity;
import cn.lysy.xiaoruan.R;
import cn.lysy.xiaoruan.activity.HomeActivity;
import cn.lysy.xiaoruan.activity.OrderActivity;
import cn.lysy.xiaoruan.base.BaseFragment;
import cn.lysy.xiaoruan.javabean.common;
import cn.lysy.xiaoruan.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener{
    View view;
    private SharedPreferences preferences;
    private final int mode=MODE_PRIVATE;
    private TextView mine_login;
    private ImageView btn_lgout;
    private ImageView btn_photo;
    private TextView crditscore;
    private ImageButton edit;
    private LinearLayout all_order;
    private LinearLayout pay_order;
    private LinearLayout comment_order;
    private LinearLayout get_order;

    private Call call;
    public MineFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        // Inflate the layout for this fragment
        setRetainInstance(true);
        view=inflater.inflate(R.layout.fragment_mine,container,false);
        intiView();
        initEvent();
        return view;
    }



    private void initEvent() {
        preferences=getActivity().getSharedPreferences("xiaoruan",mode);
        MainActivity mainActivity=new MainActivity();
        final boolean loginState=getLoginState();
        if (loginState){
            edit.setImageResource(R.drawable.ic_edit);
            btn_lgout.setImageResource(R.drawable.ic_lgout);
            crditscore.setText("信用积分");
            btn_lgout.setVisibility(View.VISIBLE);
            btn_lgout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    UserModel userModel = new UserModel();
                    String uid = preferences.getString("userid",null);
                    call = userModel.logout(uid);
                    Callback<common> commonCallback = new Callback<common>() {
                        @Override
                        public void onResponse(Call<common> call, Response<common> response) {
                            common c = response.body();
                            String s = c.getSuccess().toString();
                            if( "1".equals(s))
                            {
                                makeloginout();
                            }
                            else
                            {
                                Toast.makeText(getContext(), "注销失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<common> call, Throwable t) {
                            Toast.makeText(getContext(), "网络问题，请重试！", Toast.LENGTH_SHORT).show();
                        }
                    };
                    call.enqueue(commonCallback);

                }
            });
            Log.i(" boolean1:" , loginState+"");
        }else {
            mine_login.setText("登录/注册");
            Log.i(" boolean2:" , loginState+"");
//            mine_login.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent=new Intent(getActivity(), HomeActivity.class);
//                    startActivity(intent);
//                }
//            });
            mine_login.setOnClickListener(this);

        }

        all_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //preferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                Intent intent=new Intent(getActivity(), OrderActivity.class);
                intent.putExtra( "orderid" , 1 );//1为全部订单
                startActivity(intent);
            }
        });

        pay_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity() , OrderActivity.class);
                intent.putExtra( "orderid" , 2 );
                startActivity(intent);
            }
        });

        get_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity() , OrderActivity.class);
                intent.putExtra( "orderid", 3 );
                startActivity(intent);
            }
        });

        comment_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent( getActivity(), OrderActivity.class);
                intent.putExtra( "orderid" ,4);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initEvent();
        boolean loginState=getLoginState();
        Log.i( " boolean resume : " , ""+loginState);
    }

    @Override
    public void onClick(View view) {
        switch ( view.getId()){
            case R.id.mine_btn_login :
                startActivity(new Intent( getActivity() , HomeActivity.class));
                break;
            default:
                break;
        }
    }

    private void makeloginout(){
        SharedPreferences.Editor editor = preferences.edit();
        editor.clear();
        editor.apply();
        editor.putString( "userid" , "-1" );
        editor.commit();
        String test = preferences.getString( "userid" , "-11" );
        Log.d("text:" , test);
        Toast.makeText(getContext(), "注销成功", Toast.LENGTH_SHORT).show();
        String ss = preferences.getString( "userid" , null );
        Log.i( "result:" ,ss );
        btn_lgout.setVisibility(View.GONE);
        mine_login.setText(" 登录/注册 ");
        edit.setVisibility(view.INVISIBLE);
        crditscore.setText("");
    }

    private void intiView() {
        mine_login=view.findViewById(R.id.mine_btn_login);
        btn_lgout=view.findViewById(R.id.btn_logout);
        btn_photo=view.findViewById(R.id.mine_photo);
        edit=view.findViewById(R.id.edit);
        crditscore=view.findViewById(R.id.crditscore);
        all_order=view.findViewById(R.id.btn_order_all);
        pay_order=view.findViewById(R.id.btn_order_pay);
        comment_order=view.findViewById(R.id.btn_order_comment);
        get_order=view.findViewById(R.id.btn_order_get);
    }

    public boolean getLoginState() {
        userId=readUserId();
        return !userId.equals("-1");
    }
    private  String readUserId(){

        //读不到值，默认-1
        //String userId="1";
        String userId=preferences.getString("userid","-1");
        return userId;
    }

}
